import diff from './diff'

/**
 * 更新组件
 * @param {Object} virtualDOM 
 * @param {Function} oldComponent 
 * @param {Object} oldDOM 
 * @param {Function} container 
 */
export default function updateComponent(virtualDOM, oldComponent, oldDOM, container) {
  oldComponent.componentWillReceiveProps(virtualDOM.props)
  if(oldComponent.shouldComponentUpdate(virtualDOM.props)) {
    // 未更新前的props
    const prevProps = oldComponent.props
    oldComponent.componentWillUpdate(virtualDOM.props)
    // 组件更新
    oldComponent.updateProps(virtualDOM.props)
    // 获取组件返回的最新的 virtualDOM
    const nextVirtualDOM = oldComponent.render()
    // 更新component 组件实例对象
    nextVirtualDOM.component = oldComponent
    // 比对
    diff(nextVirtualDOM, container, oldDOM)
    oldComponent.componentDidUpdate(prevProps)
  }
}